package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.MySubClassAdapter;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.AttendanceClassData;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetMsgHttpReceiver;
import com.lzx.iteam.net.GetMySubList;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.util.DateUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 12月 09日
 * Time: 上午10:12
 *
 * @QQ : 1234567890
 * **********************************
 *
 * 我的排班页面
 */
public class AttendanceMySubActivity extends MyBaseActivity implements View.OnClickListener {

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.iv_my_sub_left)
    private ImageView mIvLeft;
    @FindViewById(id = R.id.iv_my_sub_right)
    private ImageView mIvRight;
    @FindViewById(id = R.id.tv_my_sub_date)
    private TextView mTvDate;
    @FindViewById(id = R.id.tv_my_sub_null)
    private TextView mTvNullData;
    @FindViewById(id = R.id.lv_my_sub)
    private ListView mLvSub;

    private MySubClassAdapter mAdapter;
    private ArrayList<AttendanceClassData> mData = new ArrayList<>();

    private String mGroupId;
    private String mFormId;
    private int mCurYear;
    private int mCurMonth;

    private final int MSG_GET_MY_SUB = 30001;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            switch (msg.what){
                case MSG_GET_MY_SUB:
                    waitDlgDismiss();
                    if (msg.arg1 == 0){
                        mData = (ArrayList<AttendanceClassData>) msg.obj;
                        mAdapter.bindData(mData);
                        if (mData.size() == 0){
                          mLvSub.setVisibility(View.GONE);
                          mTvNullData.setVisibility(View.VISIBLE);
                        }else{
                            mLvSub.setVisibility(View.VISIBLE);
                            mTvNullData.setVisibility(View.GONE);
                        }
                    }else{
                        Toast.makeText(mActivity, (String) msg.obj, Toast.LENGTH_LONG).show();
                    }
                    break;
            }

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.at_my_sub_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mIvLeft.setOnClickListener(this);
        mIvRight.setOnClickListener(this);

    }

    @Override
    public void initData() {
        mTvTitle.setText("我的排班");
        mAdapter = new MySubClassAdapter(this);
        mLvSub.setAdapter(mAdapter);

        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("group_id");
        mFormId = intent.getStringExtra("form_id");

        long time = System.currentTimeMillis();
        mCurYear = DateUtil.getYear(time);
        mCurMonth = DateUtil.getMonth(time);
        mTvDate.setText(mCurYear + "年" + mCurMonth + "月");
        getSubData();

    }

    /**
     * 获取班次列表
     */
    private void getSubData(){
        waitDlgShow();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_FORM_ID, mFormId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_DATE, DateUtil.getMonthBegin(mCurYear,mCurMonth) + ""));

        GetMySubList msg = new GetMySubList(mHandler.obtainMessage(MSG_GET_MY_SUB));
        msg.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_ELASTIC_ME;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(msg);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.iv_my_sub_left:
                if (mCurMonth == 1){
                    mCurMonth = 12;
                    mCurYear -= 1;
                }else{
                    mCurMonth -= 1;
                }
                mTvDate.setText(mCurYear + "年" + mCurMonth + "月");
                getSubData();
                break;
            case R.id.iv_my_sub_right:
                if (mCurMonth == 12){
                    mCurMonth = 1;
                    mCurYear += 1;
                }else{
                    mCurMonth += 1;
                }
                mTvDate.setText(mCurYear + "年" + mCurMonth + "月");
                getSubData();
                break;
        }
    }
}
